package copy.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import copy.com.R;
import copy.pojo.User;
import copy.service.UserService;
import copy.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.concurrent.TimeUnit;


@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public R<String> login(@RequestBody Map map, HttpSession session) {
        String phone = (String) map.get("phone");
        String code = map.get("code").toString();
        //手机号拿验证码 Object codes = redisTemplate.opsForValue().get(phone);
//        Object codeU = session.getAttribute(phone);
        String codeU = stringRedisTemplate.opsForValue().get(phone);
        if (codeU != null && codeU.equals(code)) {
            //验证成功 查数据库
            LambdaQueryWrapper<User> useq = new LambdaQueryWrapper<>();
            useq.eq(User::getPhone, phone);
            User user = userService.getOne(useq);
            if (user == null) {
                user = new User();
                user.setStatus(1);
                user.setPhone(phone);
                userService.save(user);
            }
            session.setAttribute("user", user.getId());
//            session.removeAttribute(phone);
            stringRedisTemplate.delete(phone);
        }
        return R.success("");
    }

    /**
     * 18888888888
     * 模拟发短信
     */
    @PostMapping("/sendMsg")
    public R sendMsg(@RequestBody User user,HttpSession session) {
        String phone = user.getPhone();
        //调用工具类生成四位随机int数
        String code = ValidateCodeUtils.generateValidateCode(4).toString();
//        session.setAttribute(phone, code);
        stringRedisTemplate.opsForValue().set(phone,code,30, TimeUnit.MINUTES);
        System.out.println(code);
        return R.success(code);
    }


    /**
     * 用户登录
     */
//    @PostMapping("/login")

}

////    @Autowired
//    private RedisTemplate redisTemplate;
/* public R<String> setMsg(@RequestBody User user) {
        String phone = user.getPhone();
        if (StringUtils.isNotEmpty(phone)) {
            String code = ValidateCodeUtils.generateValidateCode(4).toString();
            log.info("验证码{}", code);
            redisTemplate.opsForValue().set(phone, code, 60, TimeUnit.MINUTES);
            return R.success("1");
        }
        return R.error("2");
    }*/

/* public R<String> login(@RequestBody Map map, HttpSession session) {
        //从map中取出的键值对需要处理成tostring类型
        String phone = map.get("phone").toString();
        String code = map.get("code").toString();
        //手机号拿验证码
        Object codes = redisTemplate.opsForValue().get(phone);
        if (codes != null && codes.equals(code)) {
            LambdaQueryWrapper<User> userla = new LambdaQueryWrapper<>();
            userla.eq(User::getPhone, phone);
            // 发一条sql;select * from user where phone= 17389878765 and ;
            User user = userService.getOne(userla);
            if (user == null) {
                user = new User();
                user.setStatus(1);
                user.setPhone(phone);
                userService.save(user);
            }
            //不存的话拦截器那里过不了
            session.setAttribute("user", user.getId());
            //验证码真在缓存中呆着
            redisTemplate.delete(phone);
            return R.success("ok");
        }
        return R.error("no");
    }*/